df_sample<-read.csv("train_sample.csv",stringsAsFactors=F)
v.text<-tolower(df_sample$Body)

u <- grep("possible duplicate:", v.text)
id <- df_sample$Id[u]

v<-vector()

for (i in 1:length(u))
{
    x = regmatches(v.text[u[i]],regexpr("possible duplicate.*?</a>", v.text[u[i]]))
    y = regmatches(x,regexpr('\\Q\">\\E.*', x))
    z = substr(y, 3, nchar(y)-4)
    if (length(z)==0) z <- "failed"
    v<-c(v,z)
}

df.train.possible.dup <- data.frame(id=id[v!="failed"], title=v[v!="failed"])
write.csv(df.train.possible.dup,"train_poss_dup.csv", row.names=F)
############################################################################

df_sample<-read.csv("Test.csv",stringsAsFactors=F)
v.text<-tolower(df_sample$Body)

u <- grep("possible duplicate:", v.text)
id <- df_sample$Id[u]

v<-vector()

for (i in 1:length(u))
{
    x = regmatches(v.text[u[i]],regexpr("possible duplicate.*?</a>", v.text[u[i]]))
    y = regmatches(x,regexpr('\\Q\">\\E.*', x))
    z = substr(y, 3, nchar(y)-4)
    if (length(z)==0) z <- "failed"
    v<-c(v,z)
}

df.test.possible.dup <- data.frame(id=id[v!="failed"], title=v[v!="failed"])
write.csv(df.test.possible.dup,"test_poss_dup.csv", row.names=F)
